#ifndef __STEADYWIN_H
#define __STEADYWIN_H

#include "motor_def.h"
#include "rttypes.h"

typedef struct 
{
    float velocity;
    float position;
    float torque;
    rt_int32_t total_round;
    rt_uint8_t last_error_code;
    rt_uint8_t error_count;
    rt_uint8_t Error_Code;
}SteadyWinMotor_Measure_s;

typedef enum
{
    CMD_MOTOR_MODE = 0xfc,   // 使能,会响应指令
    CMD_RESET_MODE = 0xfd,   // 停止
    CMD_ZERO_POSITION = 0xfe, // 将当前的位置设置为编码器零位
    CMD_CLEAR_ERROR = 0xfb // 清除电机错误
}SteadyWinMotor_Mode_e;

typedef enum 
{
    SteadyWin_NO_ERROR = 0X00,
    FoC_frequency_too_high_ERROR = 0X01,
    Overvoltage_ERROR = 0X02,
    Undervoltage_ERROR = 0X04,
    Overtemperature_ERROR = 0X08,
    Startup_failed_ERROR = 0X10,
    Overcurrent_ERROR = 0X40,
    Software_fault_ERROR = 0X80,
}SteadyWinError_t;


typedef struct 
{
    SteadyWinMotor_Measure_s measure;
    Motor_Control_Setting_s motor_settings;
    Motor_Controller_s motor_controller;    // 电机控制器
    Motor_Type_e motor_type;        
    Motor_Working_Type_e stop_flag;
    CanMessage_t CanMessage;
    rt_uint8_t offline_manage_event;
    float torque_constant;
    rt_uint8_t gear_ratio;
    rt_slist_t list;                   // 单链表节点
}SteadyWinMotor_t;


SteadyWinMotor_t *SteadyWinMotorInit(Motor_Init_Config_s *config);

void SteadyWinMotorSetRef(SteadyWinMotor_t *motor, float ref);
void SteadyWinMotorOuterLoop(SteadyWinMotor_t *motor,Closeloop_Type_e closeloop_type);
void SteadyWinMotorEnable(SteadyWinMotor_t *motor);
void SteadyWinMotorStop(SteadyWinMotor_t *motor);
void SteadyWinMotorDecode(struct rt_can_msg can_msg);
void SteadyWinMotorcontrol(void);
void SteadyWinMotorSetCmd(SteadyWinMotor_Mode_e cmd, SteadyWinMotor_t *motor);

#endif // __STEADYWIN_H